<!DOCTYPE html>
<html>
<head>
    <title>Thermodynamics Cylinder-Piston System</title>
</head>
<body>
    <canvas id="physicsCanvas" width="550" height="750" style="border:1px solid #d3d3d3;"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 2;
    ctx.textAlign = 'left';
    ctx.textBaseline = 'middle';

    // --- Geometry ---
    const cx = 275;
    const w_narrow = 100;
    const w_wide = 200;
    const x_narrow_left = cx - w_narrow / 2;
    const x_narrow_right = cx + w_narrow / 2;
    const x_wide_left = cx - w_wide / 2;
    const x_wide_right = cx + w_wide / 2;

    const y_top = 100;
    const y_bottom = 680;
    const l_pixels = 130;
    const y_junction = y_bottom - 2 * l_pixels; // 680 - 260 = 420

    // --- Draw Cylinder ---
    // Wide part
    ctx.beginPath();
    ctx.moveTo(x_wide_left, y_top);
    ctx.lineTo(x_wide_left, y_junction);
    ctx.moveTo(x_wide_right, y_top);
    ctx.lineTo(x_wide_right, y_junction);
    ctx.stroke();
    // Narrow part
    ctx.beginPath();
    ctx.moveTo(x_narrow_left, y_junction);
    ctx.lineTo(x_narrow_left, y_bottom);
    ctx.moveTo(x_narrow_right, y_junction);
    ctx.lineTo(x_narrow_right, y_bottom);
    ctx.stroke();
    // Bottom and shoulders
    ctx.beginPath();
    ctx.moveTo(x_narrow_left, y_bottom);
    ctx.lineTo(x_narrow_right, y_bottom);
    ctx.moveTo(x_wide_left, y_junction);
    ctx.lineTo(x_narrow_left, y_junction);
    ctx.moveTo(x_wide_right, y_junction);
    ctx.lineTo(x_narrow_right, y_junction);
    ctx.stroke();

    // --- Pistons and Rod ---
    const h_piston = 12;
    const y_piston_B_top = y_bottom - l_pixels; // 550
    const h_A_wide = l_pixels / 2; // 65
    const y_piston_A_bottom = y_junction - h_A_wide; // 355
    const y_piston_A_top = y_piston_A_bottom - h_piston; // 343

    // Bottom piston
    ctx.fillRect(x_narrow_left, y_piston_B_top, w_narrow, h_piston);

    // Top piston
    ctx.fillStyle = 'black';
    ctx.fillRect(x_wide_left, y_piston_A_top, w_wide, h_piston);
    // Hatching for top piston
    ctx.save();
    ctx.strokeStyle = '#FFFFFF';
    ctx.lineWidth = 1.5;
    const hatch_step = 10;
    for (let i = x_wide_left; i < x_wide_right + h_piston; i += hatch_step) {
        ctx.beginPath();
        ctx.moveTo(i, y_piston_A_top);
        ctx.lineTo(i - h_piston, y_piston_A_bottom);
        ctx.stroke();
    }
    ctx.restore();
    ctx.fillStyle = 'black';

    // Connecting rod
    ctx.beginPath();
    ctx.moveTo(cx, y_piston_A_bottom);
    ctx.lineTo(cx, y_piston_B_top);
    ctx.stroke();

    // --- Text and Labels ---
    const baseFont = "italic 24px 'Times New Roman'";
    const subFont = "16px 'Times New Roman'";
    const normalFont = "24px 'Times New Roman'";
    
    // p₀ = mg/S
    let current_x = 200;
    const y_eq = 80;
    ctx.font = baseFont;
    ctx.fillText("p", current_x, y_eq);
    current_x += ctx.measureText("p").width;
    ctx.font = subFont;
    ctx.fillText("0", current_x, y_eq + 5);
    current_x += ctx.measureText("0").width + 5;
    ctx.font = baseFont;
    ctx.fillText("= mg/S", current_x, y_eq);

    // T₀
    current_x = 265;
    const y_T = 150;
    ctx.font = baseFont;
    ctx.fillText("T", current_x, y_T);
    current_x += ctx.measureText("T").width;
    ctx.font = subFont;
    ctx.fillText("0", current_x, y_T + 5);

    // 2m, 2S
    ctx.font = normalFont;
    ctx.textAlign = 'center';
    ctx.fillText("2m, 2S", cx, 320);
    
    // m, S
    ctx.font = baseFont;
    ctx.fillText("m, S", cx, y_piston_B_top - 20);

    // A
    ctx.font = normalFont;
    ctx.fillText("A", cx, 470);

    // B
    ctx.fillText("B", cx, (y_piston_B_top + h_piston + y_bottom) / 2);
    ctx.textAlign = 'left';

    // V₁ = 2V_B
    current_x = 420;
    const y_V = 480;
    ctx.font = baseFont;
    ctx.fillText("V", current_x, y_V);
    current_x += ctx.measureText("V").width;
    ctx.font = subFont;
    ctx.fillText("1", current_x, y_V + 5);
    current_x += ctx.measureText("1").width;
    ctx.font = baseFont;
    ctx.fillText(" = 2V", current_x, y_V);
    current_x += ctx.measureText(" = 2V").width;
    ctx.font = subFont;
    ctx.fillText("B", current_x, y_V + 5);

    // p_B = 2p₀
    current_x = 450;
    const y_p = 620;
    ctx.font = baseFont;
    ctx.fillText("p", current_x, y_p);
    current_x += ctx.measureText("p").width;
    ctx.font = subFont;
    ctx.fillText("B", current_x, y_p + 5);
    current_x += ctx.measureText("B").width;
    ctx.font = baseFont;
    ctx.fillText(" = 2p", current_x, y_p);
    current_x += ctx.measureText(" = 2p").width;
    ctx.font = subFont;
    ctx.fillText("0", current_x, y_p + 5);


    // --- Dimension Lines ---
    ctx.lineWidth = 1.5;
    const arrowSize = 8;
    const arrowHalfWidth = 4;

    // 2l dimension
    const dim_x_left = 160;
    const dim_y1_2l = y_junction; // 420
    const dim_y2_2l = y_bottom;   // 680
    ctx.beginPath();
    ctx.moveTo(dim_x_left, dim_y1_2l);
    ctx.lineTo(dim_x_left, dim_y2_2l);
    // Top arrow
    ctx.moveTo(dim_x_left - arrowHalfWidth, dim_y1_2l + arrowSize);
    ctx.lineTo(dim_x_left, dim_y1_2l);
    ctx.lineTo(dim_x_left + arrowHalfWidth, dim_y1_2l + arrowSize);
    // Bottom arrow
    ctx.moveTo(dim_x_left - arrowHalfWidth, dim_y2_2l - arrowSize);
    ctx.lineTo(dim_x_left, dim_y2_2l);
    ctx.lineTo(dim_x_left + arrowHalfWidth, dim_y2_2l - arrowSize);
    ctx.stroke();
    ctx.font = normalFont;
    ctx.fillText("2", dim_x_left - 30, (dim_y1_2l + dim_y2_2l) / 2);
    ctx.font = baseFont;
    ctx.fillText("l", dim_x_left - 18, (dim_y1_2l + dim_y2_2l) / 2);

    // l dimension
    const dim_x_right = 410;
    const dim_y1_l = y_piston_B_top; // 550
    const dim_y2_l = y_bottom; // 680
    ctx.beginPath();
    ctx.moveTo(dim_x_right, dim_y1_l);
    ctx.lineTo(dim_x_right, dim_y2_l);
    // Top arrow
    ctx.moveTo(dim_x_right - arrowHalfWidth, dim_y1_l + arrowSize);
    ctx.lineTo(dim_x_right, dim_y1_l);
    ctx.lineTo(dim_x_right + arrowHalfWidth, dim_y1_l + arrowSize);
    // Bottom arrow
    ctx.moveTo(dim_x_right - arrowHalfWidth, dim_y2_l - arrowSize);
    ctx.lineTo(dim_x_right, dim_y2_l);
    ctx.lineTo(dim_x_right + arrowHalfWidth, dim_y2_l - arrowSize);
    ctx.stroke();
    ctx.font = baseFont;
    ctx.fillText("l", dim_x_right + 15, (dim_y1_l + dim_y2_l) / 2);

</script>
</body>
</html>